package com.lizanyi.teaFriend.controller;

import com.lizanyi.teaFriend.pojo.TeaAdmin;
import com.lizanyi.teaFriend.service.AdminService;
import com.lizanyi.teaFriend.tools.Constans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class RegisterController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @RequestMapping(value = "/doregister",method = RequestMethod.POST)
    public String doregister(@RequestParam(value = "account") String account,
                             @RequestParam("password") String password,
                             @RequestParam("password1") String password1,
                             ModelMap modelMap,
                             HttpSession session) throws Exception{

        String flage=adminService.register(account,password,password1);
        System.out.println(flage);
        if(flage.equals("false")){
            modelMap.addAttribute("false1", "用户名已有，请换一个用户名");
            System.out.println("用户名已有，请换一个用户名");
            return "register";
        }else if(flage.equals("fail")){
            modelMap.addAttribute("fail","密码不一致，注册失败，请重新注册");
            return "register";
        }else if (flage.equals("empty")){
            modelMap.addAttribute("empty","用户名不能为空");
            return "register";
        }
            modelMap.addAttribute("success","注册成功");
            return "redirect:/to/login";

    }

    @RequestMapping("/to/login")
    public String tologin(){
        return "index";
    }

}
